package u.can.i.up;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import u.can.i.up.db.postgresql.ClassCount;
import u.can.i.up.db.postgresql.ClassCountDAO;
import u.can.i.up.utils.StringUtils;
import u.can.i.up.utils.SystemPackages;

public class LibsCounter {

	static Logger logger = LogManager.getLogger();

	private static List<ClassCount> mClassCountList = new ArrayList<ClassCount>();

	public static List<ClassCount> getClassCount() {
		if (mClassCountList != null && !mClassCountList.isEmpty()) {
			return mClassCountList;
		}
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader, "postgresql");
			SqlSession sqlSession = sqlSessionFactory.openSession();
			ClassCountDAO classCountDAO = sqlSession.getMapper(ClassCountDAO.class);
			mClassCountList = classCountDAO.getClassCount();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return mClassCountList;
	}

	/**
	 * @author lczgywzyy
	 * @param filter
	 *            过滤字符;
	 * @param filterCnt
	 *            过滤次数.
	 */
	public static Map<String, Integer> getClusteredClassCount(int filter, int filterCnt) {
		Map<String, Integer> classCountMap = new HashMap<String, Integer>();
		Map<String, Integer> filteredClassMap = new HashMap<String, Integer>();
		Map<String, String> mSystemPackages = SystemPackages.getSystemPackages();
		List<ClassCount> ccList = getClassCount();
		for (ClassCount cc : ccList) {
			String className = cc.getClassname();
			int count = cc.getCount();
			String simplifiedPath = StringUtils.splitPartStr(className, ".", filterCnt);
			if (!simplifiedPath.contains(".")) {
				continue;
			}
			// system app.
			if (mSystemPackages.get(simplifiedPath) != null) {
				continue;
			}
			AddPathToMap(classCountMap, simplifiedPath, count);
		}
		Iterator<Map.Entry<String, Integer>> entries = classCountMap.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry<String, Integer> entry = entries.next();
			String key = entry.getKey();
			int value = entry.getValue();
			if (value >= filter) {
				filteredClassMap.put(key, value);
			}
		}
		return filteredClassMap;
	}

	private static void AddPathToMap(Map<String, Integer> classCountMap, String simplifiedPath, int count) {
		if (classCountMap == null) {
			return;
		}
		Integer cnt;
		if ((cnt = classCountMap.get(simplifiedPath)) != null) {
			classCountMap.put(simplifiedPath, cnt + count);
		} else {
			classCountMap.put(simplifiedPath, count);
		}
	}

	public static void resetMap() {
		if (mClassCountList == null) {

		} else {
			mClassCountList.clear();
		}
	}
}
